//
//  InAppPurchaseViewController.swift
//  FrameProduct
//
//  Created by delegate on 2025/10/21.
//

import UIKit

class InAppPurchaseViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupNavi()
        
        setupUI()
        
    }
    
    private func setupNavi() {
        self.navigationItem.title = "购买套餐"
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "套餐说明", style: .plain, target: self, action: #selector(buyMessageAction))
    }
    
    private func setupUI() {
        view.addSubview(contentScrollView)
        contentScrollView.addSubviews(comboTopView, leftLineImageView, leftIconImageView, centerTitleLabel, rightIconImageView, rightLineImageView, contentImageView, personalTailorTipLabel, bottomBgImageView)
        bottomBgImageView.addSubviews(priceLabel, bottomRightBuyButton)
        
        contentScrollView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        comboTopView.snp.makeConstraints { make in
            make.top.left.equalToSuperview()
            make.width.equalTo(ScreenSize.width)
        }
        centerTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(comboTopView.snp.bottom).offset(24)
            make.centerX.equalToSuperview()
            make.height.equalTo(24)
        }
        leftIconImageView.snp.makeConstraints { make in
            make.right.equalTo(centerTitleLabel.snp.left).offset(-4)
            make.centerY.equalTo(centerTitleLabel.snp.centerY)
            make.width.height.equalTo(15)
        }
        leftLineImageView.snp.makeConstraints { make in
            make.right.equalTo(leftIconImageView.snp.left).offset(4)
            make.centerY.equalTo(leftIconImageView.snp.centerY)
            make.height.equalTo(3)
        }
        rightIconImageView.snp.makeConstraints { make in
            make.left.equalTo(centerTitleLabel.snp.right)
            make.centerY.equalTo(centerTitleLabel.snp.centerY)
            make.width.height.equalTo(15)
        }
        rightLineImageView.snp.makeConstraints { make in
            make.left.equalTo(rightIconImageView.snp.right).offset(-4)
            make.centerY.equalTo(rightIconImageView.snp.centerY)
            make.height.equalTo(3)
        }
        personalTailorTipLabel.snp.makeConstraints { make in
            make.top.equalTo(centerTitleLabel.snp.bottom).offset(0)
            make.left.equalTo(38)
            make.width.equalTo(ScreenSize.width - 29 - 38)
        }
        contentImageView.snp.makeConstraints { make in
            make.top.equalTo(personalTailorTipLabel.snp.bottom).offset(3)
            make.left.equalTo(15)
            make.width.equalTo(ScreenSize.width - 30)
        }
        bottomBgImageView.snp.makeConstraints { make in
            make.top.equalTo(contentImageView.snp.bottom).offset(15)
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.bottom.equalTo(-ScreenSize.safeAreaInsets.bottom)
            make.height.equalTo(56)
        }
        bottomRightBuyButton.snp.makeConstraints { make in
            make.right.bottom.top.equalToSuperview()
            make.width.equalTo(116)
        }
        priceLabel.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
    //=================================================================
    //                           事件处理
    //=================================================================
    // MARK: - 事件处理
    @objc private func buyMessageAction() {
        
        
        
    }
    //=================================================================
    //                            lazy
    //=================================================================
    // MARK: - lazy
    private lazy var contentScrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.backgroundColor = .clear
        scrollView.showsVerticalScrollIndicator = false;
        scrollView.showsHorizontalScrollIndicator = false
        return scrollView
    }()
    private lazy var comboTopView: InAppPurchaseComboTopView = {
        let comboView = InAppPurchaseComboTopView()
        comboView.packageTypeChangeValueBlock = {[weak self] packageType in
            switch packageType {
            case 1:
                self?.contentImageView.image = UIImage(named: "buy_vip_ privilege_img")
                self?.personalTailorTipLabel.text = ""
                self?.personalTailorTipLabel.snp.remakeConstraints { make in
                    make.top.equalTo((self?.centerTitleLabel.snp.bottom)!).offset(0)
                    make.left.equalTo(38)
                    make.width.equalTo(ScreenSize.width - 29 - 38)
                }
                self?.bottomRightBuyButton.setBackgroundImage(UIImage(named: "buy_vip_bottom_buy_bg_icon"), for: .normal)
                self?.bottomRightBuyButton.setBackgroundImage(UIImage(named: "buy_vip_bottom_buy_bg_icon"), for: .selected)
            case 2:
                self?.contentImageView.image = UIImage(named: "buy_svip_ privilege_img")
                self?.personalTailorTipLabel.text = ""
                self?.personalTailorTipLabel.snp.remakeConstraints { make in
                    make.top.equalTo((self?.centerTitleLabel.snp.bottom)!).offset(0)
                    make.left.equalTo(38)
                    make.width.equalTo(ScreenSize.width - 29 - 38)
                }
                self?.bottomRightBuyButton.setBackgroundImage(UIImage(named: "buy_svip_bottom_buy_bg_icon"), for: .normal)
                self?.bottomRightBuyButton.setBackgroundImage(UIImage(named: "buy_svip_bottom_buy_bg_icon"), for: .selected)
            case 3:
                self?.contentImageView.image = UIImage(named: "buy_svip_ personal_img.png")
                self?.personalTailorTipLabel.text = "防止频繁换IP触发平台风控，适合对稳定性和可靠性有高要求的个人/团队"
                self?.personalTailorTipLabel.snp.remakeConstraints { make in
                    make.top.equalTo((self?.centerTitleLabel.snp.bottom)!).offset(6)
                    make.left.equalTo(38)
                    make.width.equalTo(ScreenSize.width - 29 - 38)
                }
                self?.bottomRightBuyButton.setBackgroundImage(UIImage(named: "buy_personal_bottom_buy_bg_icon"), for: .normal)
                self?.bottomRightBuyButton.setBackgroundImage(UIImage(named: "buy_personal_bottom_buy_bg_icon"), for: .selected)
            default:
                self?.contentImageView.image = UIImage(named: "buy_vip_ privilege_img")
                self?.personalTailorTipLabel.text = ""
                self?.personalTailorTipLabel.snp.remakeConstraints { make in
                    make.top.equalTo((self?.centerTitleLabel.snp.bottom)!).offset(0)
                    make.left.equalTo(38)
                    make.width.equalTo(ScreenSize.width - 29 - 38)
                }
                self?.bottomRightBuyButton.setBackgroundImage(UIImage(named: "buy_vip_bottom_buy_bg_icon"), for: .normal)
                self?.bottomRightBuyButton.setBackgroundImage(UIImage(named: "buy_vip_bottom_buy_bg_icon"), for: .selected)
            }
        }
        return comboView
    }()
    private lazy var leftLineImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "buy_left_line_img")
        return imageView
    }()
    private lazy var leftIconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "buy_diamond_icon")
        return imageView
    }()
    private lazy var centerTitleLabel: UILabel = {
        let label = UILabel(frame: .zero)
        label.text = "开通会员·安全畅享互联网自由"
        label.textColor = .lxs.hex("#FFEBB5")
        label.font = .lxs.font(size: 16)
        label.textAlignment = .center
        return label
    }()
    private lazy var rightLineImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "buy_right_line_img")
        return imageView
    }()
    private lazy var rightIconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "buy_diamond_icon")
        return imageView
    }()
    private lazy var contentImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "buy_vip_ privilege_img")
        return imageView
    }()
    private lazy var personalTailorTipLabel: UILabel = {
        let label = UILabel(frame: .zero)
        label.text = ""
        label.textColor = .lxs.hex("#808080")
        label.font = .lxs.font(size: 12)
        label.textAlignment = .center
        label.numberOfLines = 0
        return label
        
    }()
    private lazy var bottomBgImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "buy_bottom_bg_img")
        imageView.isUserInteractionEnabled = true
        return imageView
    }()
    private lazy var bottomRightBuyButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle("立即购买", for: .normal)
        button.setTitle("立即购买", for: .selected)
        button.setTitleColor(.lxs.hex("#FFFFFF"), for: .normal)
        button.setTitleColor(.lxs.hex("#FFFFFF"), for: .selected)
        button.titleLabel?.font = .lxs.font(size: 16)
        button.setBackgroundImage(UIImage(named: "buy_vip_bottom_buy_bg_icon"), for: .normal)
        button.setBackgroundImage(UIImage(named: "buy_vip_bottom_buy_bg_icon"), for: .selected)
        return button
    }()
    private lazy var priceLabel: UILabel = {
        let label = UILabel(frame: .zero)
//        label.textColor = .lxs.hex("#808080")
//        label.font = .lxs.font(size: 12)
        let prefix = "普通年付：¥"
        let suffix = "288"
        let attri = NSMutableAttributedString(string: "\(prefix)\(suffix)")
        attri.addAttributes([NSAttributedString.Key.foregroundColor : UIColor.lxs.hex("#FFFFFF"), NSAttributedString.Key.font: UIFont.lxs.font(size: 12)], range: NSRange(location: 0, length: prefix.count))
        attri.addAttributes([NSAttributedString.Key.foregroundColor : UIColor.lxs.hex("#FFFFFF"), NSAttributedString.Key.font: UIFont.lxs.font(size: 24)], range: NSRange(location: prefix.count, length: suffix.count))
        label.attributedText = attri
//        label.textAlignment = .center
//        label.numberOfLines = 0
        return label
    }()
}
